<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

    <script>

        //继承的复习
        /*
        原型链继承：利用原型上的方法能够被实例使用的特性，原型如果是另一个构造函数的实例，那么对象的方法会沿着原型链追溯
        通常将 构造函数内部this添加的属性称为 实例属性
        不如意的地方：
            1. 父类构造函数的属性会称为子类共享的属性，子类在继承父类构造函数属性时无法自定义参数
        */

        function Animal(type) {
            this.type = type;
            this.hobby = ['run']
        }

        Animal.prototype.sayType = function () {
            console.log(this.type);
        }

        function Cat(name) {
            this.name = name;
        }

        Cat.prototype = new Animal('cat');

        var cat = new Cat('Tom');
        var cat2 = new Cat('Sam');

        cat.sayType(); 
        cat2.sayType(); 

        console.log(cat.type);
        console.log(cat2.type);
        cat.hobby.push('eat fish');
        console.log(cat2.hobby);

        /*
        借用构造函数继承：
            利用call方法在子类构造函数中调用父类构造函数，自定义参数，子类原型仅继承父类原型
        */

        function Animal2(type, hobby) {
            this.type = type;
            this.hobby = hobby
        }

        Animal2.prototype.sayType = function () {
            console.log(this.type);
        }

        function Cat1(name, type, hobby) {
            this.name = name;
            Animal2.call(this, type, hobby)
        }

        Cat1.prototype = new Animal2();
        Cat1.prototype.constructor = Cat1;

        var cat3 = new Cat1('Tom', 'cat', ['sat fish']);
        var cat4 = new Cat1('Sam', 'cat', ['paly water']);

        console.log(cat3);
        console.log(cat4);

        /*
            寄生式组合继承使用  创建一个空构造函数，让它的原型指向父类构造函数的原型，让子构造函数的原型等于空构造函数的实例，从而实现原型属性继承
        */


        function Animal3(type, hobby) {
            this.type = type;
            this.hobby = hobby
        }

        Animal3.prototype.sayType = function () {
            console.log(this.type);
        }

        function Temp() {
            
        }
        Temp.prototype = Animal3.prototype;

        function Cat3(name, type, hobby) {
            this.name = name;
            Animal3.call(this, type, hobby)
        }
        Cat3.prototype = new Temp();
        Cat3.prototype.constructor = Cat3;

        var cat5 = new Cat3('Tom', 'cat', ['eat fish']);
        var cat6 = new Cat3('Sam', 'cat', ['play wrater']);

        console.log(cat5);
        console.log(cat6);
    </script>
</body>
</html>